package com.dangf.hzapp.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dangf.hzapp.Config;
import com.dangf.hzapp.R;
import com.dangf.hzapp.entity.User;
import com.dangf.hzapp.util.Http;
import com.dangf.hzapp.util.Image;
import com.dangf.hzapp.util.Http.Callback;
import com.dangf.hzapp.util.Http.Method;

/**
 * 登录界面
 * 
 * @author 韦海生
 * @date 2014年8月2日
 */
public class LoginActivity extends BaseActivity {
	private EditText username_edit;
	private EditText password_edit;
	private Button login_button;
	private ImageView user_photo;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		
		initLayout();	//初始化布局
		initListener();	//初始化监听器
	}


	//初始化布局
	private void initLayout() {

		username_edit = (EditText) findViewById(R.id.username);
		password_edit = (EditText) findViewById(R.id.password);
		login_button = (Button) findViewById(R.id.login_button);
		user_photo = (ImageView) findViewById(R.id.user_photo);
		
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		user_photo.setImageBitmap(Image.getRoundedCornerBitmap(bitmap,2));
	}
	
	
	//初始化监听器
	private void initListener() {
		
		//登录按钮监听
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if("".equals(username_edit.getText()+"") || "".equals(password_edit.getText()+"")){
					Toast.makeText(LoginActivity.this, "账号密码不能为空！", Toast.LENGTH_SHORT).show();
				}else{
					//组合请求数据
					Map<String, String> data = new HashMap<String, String>();
					data.put("a", "User");
					data.put("m", "login");
					data.put("username", username_edit.getText()+"");
					data.put("password", password_edit.getText()+"");
					//开始请求
					showDialog("正在登录");
					new Http(Config.HOST_URL, Method.GET, data , new Callback() {
						@Override
						public void onSuccess(String result) {
							try {
								hideDialog();
								//解释JSON
								JSONObject json = JSON.parseObject(result);
								if("200".equals(json.getString("code"))){
									//生成JSON实体
									JSONObject userJson = json.getJSONObject("date");
									User u = new User();
									u.setId(userJson.getLong("id"));
									u.setUsername(userJson.getString("username"));
									u.setPassword(userJson.getString("password"));
									u.setName(userJson.getString("name"));
									u.setEmail(userJson.getString("email"));
									u.setPhoto(userJson.getString("photo"));
									u.setToken(userJson.getString("token"));
									
									Config.cacheUser(LoginActivity.this, u);//缓存登录信息
									
									Toast.makeText(LoginActivity.this, json.getString("msg"), Toast.LENGTH_SHORT).show();
									Intent i = new Intent(LoginActivity.this, MainActivity.class);
									startActivity(i);
									finish();
								}else{
									Toast.makeText(LoginActivity.this, json.getString("msg"), Toast.LENGTH_SHORT).show();
								}
							} catch (Exception e) {
								Toast.makeText(LoginActivity.this,"请求失败，请检查网络状态。", Toast.LENGTH_SHORT).show();
							}
							
						}
					});
					
				}
			}
		});
	}
	
	



	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==200){
			username_edit.setText(data.getStringExtra("username"));
			password_edit.setText(data.getStringExtra("password"));
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 加载菜单布局
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.login_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_register:	//进入注册页面
			startActivityForResult(new Intent(this, RegisterActivity.class), 200);
			break;
		case android.R.id.home:
			if(MainActivity.mainActivity!=null)app.outApp(this);
			else finish();
			break;
		}
		return false;
	}


	@Override
	protected String getActivityName() {
		
		return "登录";
	}
	
	
}
